package com.pdsu.xpf.graduation01.control;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.xpf.graduation01.domain.Commodity;
import com.pdsu.xpf.graduation01.dto.CommodityDTO;
import com.pdsu.xpf.graduation01.dto.OrderDTO;
import com.pdsu.xpf.graduation01.dto.PayCommodityDTO;
import com.pdsu.xpf.graduation01.service.impl.OrderServiceImpl;
import com.pdsu.xpf.graduation01.service.impl.PayCommodityServiceImpl;
import com.pdsu.xpf.graduation01.utils.JsonFormat;
import com.pdsu.xpf.graduation01.utils.MyDateUtil;
import com.pdsu.xpf.graduation01.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RequestMapping("order")
@Controller
public class OrderControl {

    final
    OrderServiceImpl orderService;

    final
    PayCommodityServiceImpl payCommodityService;

    public OrderControl(PayCommodityServiceImpl payCommodityService, OrderServiceImpl orderService) {
        this.payCommodityService = payCommodityService;
        this.orderService = orderService;
    }

    @ResponseBody
    @RequestMapping("/createOrder")
    public Result createOrder(HttpServletRequest request) throws Exception {

        String data= request.getParameter("data");
        List<PayCommodityDTO> commodityDTOS= JsonFormat.json2list(data,PayCommodityDTO.class);
        //创建订单
       //删除购物车中内容
        int uid = Integer.parseInt(request.getParameter("uid"));
        //前缀

        String type_http = request.getParameter("type_http");
        String order_number= MyDateUtil.createOrderNumberByTime();
        for (PayCommodityDTO p:commodityDTOS){
            System.out.println(p.getCnumber()+"c");
            System.out.println(p.getUcnumber()+"uc");
            Integer j = orderService.createOrder(uid,p.getCid(),p.getCname(),p.getUcnumber(),p.getImage(),order_number);
            if (type_http.equals("1")){
                Integer i = payCommodityService.removePayCommodityByUIDAndCID(uid,p.getCid());

            }

        }
        return Result.success(order_number);
    }

    //getOrderDTOListByOrderUID


    @ResponseBody
    @RequestMapping("/getOrderDTOListByOrderUID")
    public Result getOrderDTOListByOrderUID(HttpServletRequest request,int uid) throws Exception {
         List<OrderDTO> orderDTOS= orderService.getOrderDTOListByOrderUID(uid);
         String data_json = JsonFormat.ListToJson(orderDTOS);
        System.out.println(data_json+"*-*-*--*-");
        return Result.success(data_json);
    }
    //order_number
    @ResponseBody
    @RequestMapping("/payorder")
    public Result payorder(HttpServletRequest request,String order_number) throws Exception {

        System.out.println("order_number");
        Integer num = orderService.updateOrderStateByOrderNumber(order_number);
        System.out.println(order_number);
        return Result.success();
    }
}
